# Maintainer: David Runge <dvzrv@archlinux.org>

pkgbase=zam-plugins
pkgname=(zam-plugins{,-{clap,ladspa,lv2,standalone,vst,vst3}})
pkgver=4.2
_commit=87bf2cdebb901c22ad8e63835ec57f5eb8e966c6  # refs/tags/4.2
pkgrel=1
pkgdesc="Collection of audio plugins for high-quality processing"
arch=(loong64 x86_64)
url="https://github.com/zamaudio/zam-plugins"
license=(GPL-2.0-or-later)
makedepends=(
  gcc-libs
  glibc
  gendesk
  git
  ladspa
  libglvnd
  libsamplerate
  libx11
  jack
  lv2
  zita-convolver
)
source=(
  "git+$url#tag=$_commit?signed"
  git+https://github.com/distrho/dpf.git
  distrho-pugl::git+https://github.com/distrho/pugl.git
  $pkgname.directory
  $pkgname.menu
)
sha512sums=('SKIP'
            'SKIP'
            'SKIP'
            'b7aa3170c14e75e1ec9aa19827a353d126a70e729491a8947b86748eb6c97489c57cb697505f209129834b837beadbbd96e8306fbd7b78cc7cfb95cd7d8b964d'
            '0aa04bc5cc566ce616728e96fc528c4e7d43cc275cf3e58ad4005a195fbce72793497c2abc515a3926c8cc196e2e29a5534d4fb2ea3c1f348a6f1df3f8a24740')
b2sums=('SKIP'
        'SKIP'
        'SKIP'
        '7b00a157f982abee0abce18e6aaf88f3464f734653ad8efcaf16c8aa8daa4d81e5648381aa16e46461d134bba536fe8f81bee3f5f0456d22f47be5f4c2f2878e'
        '830efd881fe5f66ef51e13cf9a4026446aa185c8b161209e97212652771d1938c26eed4d9eb81849256f600ec01029f6a50dd98436856fa910aef11d22bc5715')
validpgpkeys=('B86F8ABAEDB92DF68AE2BE40577C1739585FA920') # Damien Zammit <damien@zammit.org>

_names=(
  zamaximx2
  zamulticomp
  zamulticompx2
  zamautosat
  zamcomp
  zamcompx2
  zamdelay
  zamdynamiceq
  zameq2
  zamgeq31
  zamgate
  zamgatex2
  zamgrains
  zamheadx2
  zamphono
  zamtube
  zamverb
)

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}


prepare() {
  declare -A exec_names=(
    ["zamaximx2"]="ZaMaximX2"
    ["zamulticomp"]="ZaMultiComp"
    ["zamulticompx2"]="ZaMultiCompX2"
    ["zamautosat"]="ZamAutoSat"
    ["zamcomp"]="ZamComp"
    ["zamcompx2"]="ZamCompX2"
    ["zamdelay"]="ZamDelay"
    ["zamdynamiceq"]="ZamDynamicEQ"
    ["zameq2"]="ZamEQ2"
    ["zamgeq31"]="ZamGEQ31"
    ["zamgate"]="ZamGate"
    ["zamgatex2"]="ZamGateX2"
    ["zamgrains"]="ZamGrains"
    ["zamheadx2"]="ZamHeadX2"
    ["zamphono"]="ZamPhono"
    ["zamtube"]="ZamTube"
    ["zamverb"]="ZamVerb"
  )
  declare -A comments=(
    ["zamaximx2"]="Acts as a brickwall limiter for mastering in its default state, but can also be tweaked to raise the average level as a stereo maximizer without ever clipping"
    ["zamulticomp"]="Mono multiband compressor, with 3 adjustable bands."
    ["zamulticompx2"]="Stereo version of ZaMultiComp, with individual threshold controls for each band and real-time visualisation of comp curves."
    ["zamautosat"]="An automatic saturation plugin, has been known to provide smooth levelling to live mic channels."
    ["zamcomp"]="A powerful mono compressor strip"
    ["zamcompx2"]="Stereo version of ZamComp with knee slew control"
    ["zamdelay"]="A simple feedback delay unit with sync-to-host BPM feature and filter."
    ["zamdynamiceq"]="A dynamic equalizer that changes its gain based on detecting a narrow band of frequencies."
    ["zameq2"]="Two band parametric equaliser with high and low shelving circuits."
    ["zamgeq31"]="31 band graphic equaliser, good for eq of live spaces, removing unwanted noise from a track etc."
    ["zamgate"]="Gate plugin for ducking low gain sounds."
    ["zamgatex2"]="Gate plugin for ducking low gain sounds, stereo version."
    ["zamgrains"]="Granular Synthesizer"
    ["zamheadx2"]="HRTF acoustic filtering plugin for directional sound."
    ["zamphono"]="A collection of phono filters for restoring vinyl records, or preparing to cut new ones."
    ["zamtube"]="Wave digital filter physical model of a triode tube amplifier stage, with modelled tone stacks from real guitar amplifiers"
    ["zamverb"]="Reverb"
  )
  declare -A generic=(
    ["zamaximx2"]="Maximizer and brickwall limiter"
    ["zamulticomp"]="Mono Multiband Compressor"
    ["zamulticompx2"]="Stereo Multiband Compressor"
    ["zamautosat"]="Automatic Saturation"
    ["zamcomp"]="Mono Compressor"
    ["zamcompx2"]="Stereo Compressor"
    ["zamdelay"]="Delay"
    ["zamdynamiceq"]="Dynamic Equalizer"
    ["zameq2"]="2 Band Parametric Equalizer"
    ["zamgeq31"]="31 Band Graphic Equalizer"
    ["zamgate"]="Mono Gate"
    ["zamgatex2"]="Stereo Gate"
    ["zamgrains"]="Granular Synthesizer"
    ["zamheadx2"]="HRTF Acoustic Filtering"
    ["zamphono"]="Phono Filters"
    ["zamtube"]="Tube Amplifier"
    ["zamverb"]="Reverb"
  )

  for name in "${_names[@]}"; do
    gendesk -n \
            --pkgname com.zamaudio.$name \
            --name $name \
            --exec "${exec_names[$name]}" \
            --pkgdesc "${comments[$name]}" \
            --genericname "${generic[$name]}"
  done

  cd $pkgname
  git submodule init
  git config submodule.dpf.url "$srcdir/dpf"
  git -c protocol.file.allow=always submodule update
  cd dpf
  git submodule init
  git config submodule.dgl/src/pugl-upstream.url "$srcdir/distrho-pugl"
  git -c protocol.file.allow=always submodule update
}

build() {
  cd $pkgname
  export HAVE_ZITA_CONVOLVER=true
  make
}

package_zam-plugins() {
  depends=(zam-plugins-{clap,ladspa,lv2,standalone,vst,vst3})

  cd $pkgname
  make DESTDIR="$pkgdir/" PREFIX=/usr install

  # have find-libdeps resolve dependencies properly:
  # https://gitlab.archlinux.org/archlinux/devtools/-/issues/102
  find "$pkgdir" -type f \( -iname "*.clap" -or -iname "*.so" \) -exec chmod +x {} +

  (
    cd "$pkgdir"
    _pick zam-plugins-clap usr/lib/clap
    _pick zam-plugins-standalone usr/bin/
    _pick zam-plugins-ladspa usr/lib/ladspa
    _pick zam-plugins-lv2 usr/lib/lv2
    _pick zam-plugins-vst usr/lib/vst/
    _pick zam-plugins-vst3 usr/lib/vst3/
  )
}

package_zam-plugins-clap() {
  pkgdesc+=" - CLAP plugins"
  depends=(
    clap-host
    dbus libdbus-1.so
    gcc-libs
    glibc
    libglvnd libGL.so
    libsamplerate libsamplerate.so
    libx11
    libxext
    zita-convolver libzita-convolver.so
  )
  groups=(clap-plugins pro-audio)

  mv -v $pkgname/* "$pkgdir"
  install -vDm 644 $pkgbase/{README.md,changelog} -t "$pkgdir/usr/share/doc/$pkgname/"
}

package_zam-plugins-standalone() {
  pkgdesc+=" - standalone"
  depends=(
    alsa-lib libasound.so
    dbus libdbus-1.so
    gcc-libs
    glibc
    jack
    libglvnd libGL.so
    libsamplerate libsamplerate.so
    libx11
    libxext
    zita-convolver libzita-convolver.so
  )

  mv -v $pkgname/* "$pkgdir"
  install -vDm 644 ./*.desktop -t "$pkgdir/usr/share/applications"
  install -vDm 644 $pkgbase.menu -t "$pkgdir/etc/xdg/menus/applications-merged/"
  install -vDm 644 $pkgbase.directory -t "$pkgdir/usr/share/desktop-directories/"
  install -vDm 644 $pkgbase/{README.md,changelog} -t "$pkgdir/usr/share/doc/$pkgname/"
}

package_zam-plugins-ladspa() {
  pkgdesc+=" - ladspa plugins"
  depends=(
    gcc-libs
    glibc
    ladspa-host
  )
  groups=(ladspa-plugins pro-audio)

  mv -v $pkgname/* "$pkgdir"
  install -vDm 644 $pkgbase/{README.md,changelog} -t "$pkgdir/usr/share/doc/$pkgname/"
}

package_zam-plugins-lv2() {
  pkgdesc+=" - LV2 plugins"
  depends=(
    dbus libdbus-1.so
    gcc-libs
    glibc
    libglvnd libGL.so
    libsamplerate libsamplerate.so
    libx11
    libxext
    lv2-host
    zita-convolver libzita-convolver.so
  )
  groups=(lv2-plugins pro-audio)

  mv -v $pkgname/* "$pkgdir"
  install -vDm 644 $pkgbase/{README.md,changelog} -t "$pkgdir/usr/share/doc/$pkgname/"
}

package_zam-plugins-vst() {
  pkgdesc+=" - VST plugins"
  depends=(
    dbus libdbus-1.so
    gcc-libs
    glibc
    libglvnd libGL.so
    libsamplerate libsamplerate.so
    libx11
    libxext
    vst-host
    zita-convolver libzita-convolver.so
  )
  groups=(vst-plugins pro-audio)

  mv -v $pkgname/* "$pkgdir"
  install -vDm 644 $pkgbase/{README.md,changelog} -t "$pkgdir/usr/share/doc/$pkgname/"
}

package_zam-plugins-vst3() {
  pkgdesc+=" - VST3 plugins"
  depends=(
    dbus libdbus-1.so
    gcc-libs
    glibc
    libglvnd libGL.so
    libsamplerate libsamplerate.so
    libx11
    libxext
    vst3-host
    zita-convolver libzita-convolver.so
  )
  groups=(vst3-plugins pro-audio)

  mv -v $pkgname/* "$pkgdir"
  install -vDm 644 $pkgbase/{README.md,changelog} -t "$pkgdir/usr/share/doc/$pkgname/"
}

